﻿using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Implement;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.InventoryAudits;
using Management.Systeam.Read.Apis.Application.Command.LedgerManagements;
using MediatR;
using System.Text;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.LedgerManagements
{
    public class FirstLedgerManagementsCommandHandler : IRequestHandler<FirstLedgerManagementsCommand, ApiResult<LedgerManagement>>
    {
        IBaseRepository<LedgerManagement> LedgerManagement;
        public DatabaseContext db;

        public FirstLedgerManagementsCommandHandler(IBaseRepository<LedgerManagement> LedgerManagement, DatabaseContext db)
        {
            this.LedgerManagement = LedgerManagement;
            this.db = db;
        }

        public async Task<ApiResult<LedgerManagement>> Handle(FirstLedgerManagementsCommand request, CancellationToken cancellationToken)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM LedgerManagement");
            sb.Append(" WHERE IsDel LIKE '%" + 0 + "%'");

            sb.Append(" AND LedgerInfoManagement_Id = '" + request.id + "'");

            sb.Append(" ORDER BY LedgerInfoManagement_Id DESC");
            var list = await LedgerManagement.QueryAsync(sb.ToString());

            return new ApiResult<LedgerManagement>
            {
                Code = ApiEnum.success,
                Message = "获取用户列表成功",
                Data = list.FirstOrDefault()
            };
        }
    }
}
